package com.shopping.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import com.shopping.bean.Goods;
import com.shopping.dao.GoodsDao;
import com.shopping.superDao.SuperDao;
import com.shopping.superDao.impl.SuperDaoImpl;
import com.shopping.util.DBUtil;

public class GoodsDaoImpl implements GoodsDao {

	JdbcTemplate jt = new JdbcTemplate(DBUtil.getDataSource());
	SuperDao sd = new SuperDaoImpl();
	
	@Override
	public List<Goods> selectGoodsByKeyword(String keyword, int pageNum, int pageSize) {
		String sql = "SELECT * FROM goods WHERE gName LIKE ? OR gType LIKE ? LIMIT ?,?";
		return jt.query(sql, new BeanPropertyRowMapper<Goods>(Goods.class),"%"+keyword+"%","%"+keyword+"%",(pageNum-1)*pageSize,pageSize);
	}

	@Override
	public int selectTotalRows(String keyword) {
		String sql = "SELECT count(1) FROM goods WHERE gName LIKE ? OR gIntro LIKE ?";
		return jt.queryForObject(sql, Integer.class,"%"+keyword+"%","%"+keyword+"%");
	}

	@Override
	public List<Goods> selectGoods(String gType,int pageNum,int pageSize) {
		String sql = "SELECT * FROM goods WHERE gType = ? LIMITE ?,?";
		return jt.query(sql, new BeanPropertyRowMapper<Goods>(Goods.class),gType,(pageNum-1)*pageSize,pageSize);
	}

	@Override
	public Map<String, Object> selectByName(String name) {
		return sd.selectByName(name, "goods", "gName");
	}
	@Override
	public int insertGoods(Goods goods) {
		String sql = "insert into goods(gName,gPrice,gType,gIntro,gImage,gIf,busineId) values(?,?,?,?,?,?,?)";
		System.out.println(goods.getBusineId());
		jt.update(sql,goods.getgName(),goods.getgPrice(),goods.getgType(),goods.getgIntro(),goods.getgImage(),goods.getgIf(),goods.getBusineId());
		System.out.println(sql);
		return 0;
	}
	@Override
	public int selectGoodsTotalRows() {
		String sql = "select count(1) from goods where 1=1 ";
		return jt.queryForObject(sql, Integer.class);
	}
	@Override
	public int insertGoodsOnShopCar(int userId,Goods goods,int goodsNum) {
		String sql = "insert into shopCar(userId,goodsId,goodsNum) values(?,?,?)";
		return jt.update(sql,userId,goods.getGoodsId(),goodsNum);
		
	}
	@Override
	public Goods selectGoodsById(int goodsId) {
		String sql = "select * from goods where goodsId = ?";
		List<Goods> list = jt.query(sql, new BeanPropertyRowMapper<Goods>(Goods.class),goodsId);
		return list.get(0);
	}
	
	@Override
	public List<Goods> selectGoods(Map<String, String[]> condition) {
		String sql = "select * from goods where 1=1 ";
		int pageNum = 1;
		int pageSize = 5;
		
		List list = new ArrayList();
		for(String key:condition.keySet()) {
			if("keyword".equals(key) && !"".equals(condition.get(key)[0])) {
				sql += "and gType LIKE ? ";
				list.add("%"+condition.get(key)[0]+"%");
			}
			
			if("keyword".equals(key) && !"".equals(condition.get(key)[0])) {
				sql += "or gName LIKE ? ";
				list.add("%"+condition.get(key)[0]+"%");
			}
			
		}
		try {
			pageNum = Integer.parseInt(condition.get("pageNum")[0]);
		}catch(Exception e) {
			
		}
		try {
			pageSize = Integer.parseInt(condition.get("pageSize")[0]);
		}catch(Exception e) {
			
		}
		sql += "LIMIT ?,?";
		list.add((pageNum-1)*pageSize);
		list.add(pageSize);
		
		List<Goods> goods = jt.query(sql, new BeanPropertyRowMapper<Goods>(Goods.class),list.toArray());
		return goods;
	}
	
	@Override
	public int selectTotalRows(Map<String, String[]> condition) {
		String sql ="SELECT count(1) FROM goods WHERE 1 = 1 ";
		List params = new ArrayList(); //由于sql语句是动态的 ?的个数 不确定，所以没拼接一个sql 我们就先把这个参数放在 list集合当中
		for(String key: condition.keySet()) {
			if("keyword".equals(key) && !"".equals(condition.get(key)[0])) {
				sql += "and gType like ? ";
				params.add(condition.get(key)[0]);
			}
			if("keyword".equals(key) && !"".equals(condition.get(key)[0])) {
				sql += "or gName LIKE ? ";
				params.add("%"+condition.get(key)[0]+"%");
			}
			
		}
		
			return jt.queryForObject(sql, Integer.class,params.toArray());
	}
	
	@Override
	public int deleteGoodsById(int goodsId) {
		String sql = "delete from goods where goodsId = ?";
		return jt.update(sql,goodsId);
	}

	@Override
	public int updateGoods(Goods newGoods, Goods oldGoods) {
		String sql = "update goods set gName = ?,gPrice = ?,gType = ?,gImage = ?,gIntro = ?,gIf = ?,busineId = ? where gId = ? ";
		return jt.update(sql,newGoods.getgName(),newGoods.getgPrice(),newGoods.getgType(),newGoods.getgImage(),newGoods.getgIntro(),newGoods.getgIf(),newGoods.getBusineId(),oldGoods.getGoodsId());
	}

}
